package com.wjj.happytime.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import com.jiongbull.jlog.JLog;

/**
 * 数据库助手类
 * Created by junjie.wang on 2017/5/2.
 */

public class MyDbHelper extends SQLiteOpenHelper {

  public MyDbHelper(Context context, String name,
      CursorFactory factory, int version) {
    super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS tableHappy ("
        + "_id integer primary key autoincrement,"
        +"format text,"
        + " thumb text,"
        + "name text,"
        + "content text,"
        + "low_loc text,"
        + "high_loc text,"
        + "low_url text,"
        + "pic_url text)");
    JLog.e("创建tableHappy成功");
    db.execSQL("CREATE TABLE IF NOT EXISTS tableListen ("
        + "_id integer primary key autoincrement,"
        + "coverLarge text,"
        + "name text,"
        + "playUrl text)");
    JLog.e("创建tableListen成功");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int i, int i1) {
/**
 * 如果发现数据库中已经存在 tableHappy 表或 tableListen 表
 * 就将这两张表删除掉，然后调用 onCreate() 方法重新创建
 * 如果在创建表时发现表已经存在，就会直接报错
 */
    db.execSQL("DROP TABLE IF EXISTS tableHappy");
    db.execSQL("DROP TABLE IF EXISTS tableListen");
    onCreate(db);
  }
}
